package com.zh.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zh.common.constant.AuthConstant;
import com.zh.web.domain.auth.dto.AddRole2UserDto;
import com.zh.web.domain.auth.dto.DelRole2UserDto;
import com.zh.auth.mapper.UserRoleMapper;
import com.zh.auth.service.UserRoleService;
import com.zh.common.exception.BusinessException;
import com.zh.web.domain.auth.entity.UserRole;
import com.zh.web.enums.ProtectedRole;
import com.zh.web.enums.Status;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.aop.framework.AopContext;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class UserRoleServiceImpl extends ServiceImpl<UserRoleMapper, UserRole> implements UserRoleService {

    private final UserRoleMapper userRoleMapper;

    /**
     * 添加角色到用户
     *
     * @param addRole2UserDto 参数
     * @return Boolean
     */
    public String addRole2User(AddRole2UserDto addRole2UserDto) {
        UserRoleService proxy = (UserRoleService) AopContext.currentProxy();
        UserRole userRole = BeanUtil.copyProperties(addRole2UserDto, UserRole.class);
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getUserId, userRole.getUserId());
        wrapper.eq(UserRole::getRoleCode, userRole.getRoleCode());
        if (userRoleMapper.selectCount(wrapper) > 0) {
            throw new BusinessException("角色已存在!");
        }
        proxy.removeIfMarkedAsDeleted(List.of(addRole2UserDto.getUserId()));
        return userRoleMapper.insert(userRole) > 0 ? "添加成功" : "添加失败";
    }

    @Override
    public String delRole2User(DelRole2UserDto delRole2UserDto) {
        if (delRole2UserDto.getUserId().equals(AuthConstant.ADMIN_ID)
                && delRole2UserDto.getRoleCode().equals(ProtectedRole.ADMIN.getRoleCode())) {
            throw new BusinessException("管理员该角色不允许删除");
        }
        LambdaUpdateWrapper<UserRole> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserRole::getUserId, delRole2UserDto.getUserId());
        wrapper.eq(UserRole::getRoleCode, delRole2UserDto.getRoleCode());
        boolean success = userRoleMapper.delete(wrapper) > 0;
        if (success) {
            return "删除成功";

        } else {
            throw new BusinessException("删除失败");
        }
    }

    @Override
    public List<UserRole> listUserRole() {
        LambdaUpdateWrapper<UserRole> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(UserRole::getStatus, Status.ENABLE);
        return userRoleMapper.selectList(wrapper);
    }

    @Override
    public List<UserRole> selectRoleByUserId(Long userId) {
        LambdaQueryWrapper<UserRole> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserRole::getUserId, userId);
        return userRoleMapper.selectList(queryWrapper);
    }

    @Override
    public void removeIfMarkedAsDeleted(List<Long> userId) {
        userRoleMapper.permanentlyDeleteByIds(userId);
    }
}
